#include "ConcurrentAlloc.h"
void Alloc1()
{
	for (size_t i = 0; i < 5; ++i)
	{
		void* ptr = ConcurrentAlloc(6);
	}
}
void Alloc2()
{
	for (size_t i = 0; i < 5; ++i)
	{
		void* ptr = ConcurrentAlloc(7);
	}
}
void Uni1Test()
{
	std::thread t1(Alloc1); t1.join();
	std::thread t2(Alloc2); t2.join();
}
void TestConCurrentAlloc1()
{
	//ConcurrentAlloc(7);
	//ConcurrentAlloc(8);
	//ConcurrentAlloc(6);
	//ConcurrentAlloc(1);
	//ConcurrentAlloc(5);
	//ConcurrentAlloc(4);
	//ConcurrentAlloc(2);
	//ConcurrentAlloc(3);
	void* p1 = ConcurrentAlloc(6);
	void* p2 = ConcurrentAlloc(8);
	void* p3 = ConcurrentAlloc(1);
	void* p4 = ConcurrentAlloc(7);
	void* p5 = ConcurrentAlloc(8);
	void* p6 = ConcurrentAlloc(8);
void* p7 = ConcurrentAlloc(8);


	cout << p1 << endl;
	cout << p2 << endl;
	cout << p3 << endl;
	cout << p4 << endl;
	cout << p5 << endl;
	cout << p6 << endl;
	cout << p7 << endl;


	ConcurrentDelloc(p1);
	ConcurrentDelloc(p2);
	ConcurrentDelloc(p3);
	ConcurrentDelloc(p4);
	ConcurrentDelloc(p5);
	ConcurrentDelloc(p6);
	ConcurrentDelloc(p7);


}
void TestConCurrentAlloc2()
{
	for (int i = 1; i <= 1024; i++)
	{
		ConcurrentAlloc(i);
	}
}
void TestAddressShift()
{
	PageId id1 = 2000;
	PageId id2 = 2001;
	char* p1 = (char*)(id1 << PAGE_SHIFT);
	char* p2 = (char*)(id2 << PAGE_SHIFT);
	int i = 1;
	while (p1 < p2)
	{
		cout << (void*)p1 << ":" << ((PageId)p1 >> PAGE_SHIFT) << " " << i << endl;
		i++;
		p1 += 8;
	}
}

void MultiThreadAlloc1()
{
	std::vector<void*> v;
	for (size_t i = 0; i < 7; ++i)
	{
		void* ptr = ConcurrentAlloc(6);
		v.push_back(ptr);
	}

	for (auto e : v)
	{
		ConcurrentDelloc(e);
	}
}

void MultiThreadAlloc2()
{
	std::vector<void*> v;
	for (size_t i = 0; i < 7; ++i)
	{
		void* ptr = ConcurrentAlloc(16);
		v.push_back(ptr);
	}

	for (auto e : v)
	{
		ConcurrentDelloc(e);
	}
}

void TestMultiThread()
{
	std::thread t1(MultiThreadAlloc1);
	std::thread t2(MultiThreadAlloc2);

	t1.join();
	t2.join();
}
void BigAlloc()
{
	void* p1 = ConcurrentAlloc(257 * 1024);
	ConcurrentDelloc(p1);

	void* p2 = ConcurrentAlloc(129 * 8 * 1024);
	ConcurrentDelloc(p2);
}

/*int main()
{
	//Uni1Test();
	TestConCurrentAlloc1();
	//TestConCurrentAlloc2();
	//TestAddressShift();
	//TestMultiThread();
	BigAlloc();
}*/